﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.PO;
using Com.CMS263.Util;
using Com.CMS263.BLL;
using System.IO;

namespace Com.CMS263.Web.manage.ManageSingleArt
{
    public partial class ModArt : System.Web.UI.Page
    {
        public ExtAttributeBLL extbll = new ExtAttributeBLL();
        public IList<ExtAttribute> extList = null;
        public ArtTypeBLL atbll = new ArtTypeBLL();
        public int page = 1;
        public int pid;
        public ArtBaseBLL abll = new ArtBaseBLL();
        public ArtBase ab = new ArtBase();
        public ExtValueBLL evbll = new ExtValueBLL();
        public ExtValue extvalue = new ExtValue();
        public Com.CMS263.PO.ArtType type = null;
        public int menuId ;
        public int id;
        public string menuTitle = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            pid = Convert.ToInt32(Request.QueryString.Get("pid"));
            extList = extbll.GetExtAttributeByTypeId(pid);
            menuId = Convert.ToInt32(Request["menuId"]);
            this.hdfMenuId.Value = menuId.ToString();
            type = atbll.GetArtTypeById(menuId);
            page = string.IsNullOrEmpty(Request["page"]) ? 1 : int.Parse(Request["page"]);
            menuTitle = type.typeName;
            
            this.hdfTypeId.Value = ab.typeId.ToString();
            if (!Page.IsPostBack)
            {
                //根据父类类型盘点是否出默认结果
               
                if (menuId != 0)
                {

                    ab = abll.GetArtBaseByMenuId(menuId);
                    string tit = ab.title;
                    if (!string.IsNullOrEmpty(tit))
                    {
                        if (tit.IndexOf("<b>") > 0)
                        {
                            this.chkStrong.Checked = true;


                        }
                        if (tit.IndexOf("color:red") > 0)
                        {
                            this.chkRed.Checked = true;
                        }
                    }


                }
                else
                {
                    Response.Redirect("AddBaseArt.aspx");
                }
            }
            id = ab.id;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ArtBaseBLL abbll = new ArtBaseBLL();
            //文件按天存储，没有目录就自己创建
            string strDay = DateTime.Now.ToString("yyyy-MM-dd");
            string savePath = Server.MapPath("/ArtUpFile/" + strDay);
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }


            string addDate = Request["addDate"];
           
            //int typeId = int.Parse(this.hdfTypeId.Value);
            menuId = Convert.ToInt32(this.hdfMenuId.Value);
            ab = abbll.GetArtBaseByMenuId(menuId);

            ab.addDate = DateTime.Parse(addDate);
            ab.clickCount = int.Parse(Request["clickCount"]);
          
            ab.isBest = 0;
            ab.isShow = 1;
            ab.orderNum = 0;
            //ab.typeId =Convert.ToInt32(type);
            string title = Request["title"];
            if (!string.IsNullOrEmpty(title))
            {
                if (chkStrong.Checked)
                {
                    title = "<b>" + title + "</b>";

                }
                if (this.chkRed.Checked)
                {
                    title = "<span style='color:red'>" + title + "</span>";
                }
            }
            else
            { StringUtil.Alert("请输入标题", "-1"); }
            ab.title = title;
            IList<ExtAttribute> attrList = extbll.GetExtAttributeByTypeId(pid);
            IList<ExtValue> valList = new List<ExtValue>();
            //循环属性列表得到属性值并天数值列表
            foreach (ExtAttribute item in attrList)
            {
                string val = "";
                int notNull = item.notNull;
                if (item.attrType == 4 || item.attrType == 5)
                {
                    HttpPostedFile hpf = Request.Files[item.attrName];
                    if (hpf.ContentLength > 5)
                    {
                        val = strDay + "/" + UpLoadFileUtil.UpLoadFile(Request.Files[item.attrName], savePath);
                    }
                }
                else
                {
                    if (notNull == 1)
                    {
                        if (string.IsNullOrEmpty(Request[item.attrName]))
                        {
                            StringUtil.Alert(item.attrTxtName + "是必填的", "-1");
                            return;
                        }
                    }
                    val = Request[item.attrName];
                }

                ExtValue ev = evbll.GetExtValueById(ab.id, item.id);

                if (ev != null)
                {
                    foreach (ExtAttribute exa in extList)
                    {

                        if (exa.id == ev.attrId)
                        {
                            if (!string.IsNullOrEmpty(val))
                            {
                                ev.val = val;
                            }
                        }
                    }
                    valList.Add(ev);
                }
                else
                {
                    ExtValue evv = new ExtValue();
                    evv.baseArtId = ab.id;
                    evv.attrId = item.id;
                    evv.val = "";
                    evbll.Add(evv);
                }

            }
            ab.valueList = valList;

            abbll.UpdateArt(ab);

            AdminLoginInfo ali = AdminLoginCookie.GetLoginInfo();
            ArtTypeBLL atbll = new ArtTypeBLL();
            OperationLogBLL olbll = new OperationLogBLL();
            OperationLog ol = new OperationLog();
            ol.AddDate = DateTime.Now;
            ol.Ip = StringUtil.GetIP();
            ol.ModuleName = atbll.GetArtTypeById(ab.menuId).typeName;
            ol.Contents = "【修改】" + ab.title;
            ol.UserName = ali.UserName;
            olbll.Add(ol);

            StringUtil.Alert("修改成功","ModArt.aspx?pid="+pid+"&menuId="+menuId);
        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {

        }
        public string GetText(object obj)
        {
            return obj.ToString().Replace("<span style='color:red'>", "").Replace("</span>", "").Replace("<b>", "").Replace("</b>", "");
        }
    }
   
}
